﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using EntityFrameworkStudy.Studys;

namespace EntityFrameworkStudy.Studys.Studys
{
    class KeyMappingStudy
    {

        private static void 已删除情况的主键映射()
        {
            Console.WriteLine("已删除情况的主键映射");

            using (var context = new StudyContext())
            {
                {
                    var order = context.Orders.First();
                    context.Orders.Remove(order);
                    Console.WriteLine(context.Entry(order).State);
                }

                {
                    var order = context.Orders.Find(1);
                    Console.WriteLine(context.Entry(order).State);
                }

                {
                    var order = context.Orders.First();
                    Console.WriteLine(context.Entry(order).State);
                }

                {
                    foreach (var item in context.ChangeTracker.Entries())
                    {
                        Console.WriteLine("{0}：{1}", item.Entity.GetHashCode(), item.State);
                    }
                }
            }
        }

        private static void 已修改情况的主键映射()
        {
            Console.WriteLine("已修改情况的主键映射");

            using (var context = new StudyContext())
            {
                {
                    var order = context.Orders.First();
                    order.Product = string.Empty;
                    Console.WriteLine(context.Entry(order).State);
                }

                {
                    var order = context.Orders.Find(1);
                    Console.WriteLine(context.Entry(order).State);
                }

                {
                    var order = context.Orders.First();
                    Console.WriteLine(context.Entry(order).State);
                }

                {
                    foreach (var item in context.ChangeTracker.Entries())
                    {
                        Console.WriteLine("{0}：{1}", item.Entity.GetHashCode(), item.State);
                    }
                }
            }
        }

        private static void 已脱钩情况的主键映射()
        {
            Console.WriteLine("已脱钩情况的主键映射");

            using (var context = new StudyContext())
            {
                {
                    var order = context.Orders.First();
                    context.Entry(order).State = System.Data.EntityState.Detached;
                    Console.WriteLine(context.Entry(order).State);
                }

                {
                    var order = context.Orders.Find(1);
                    Console.WriteLine(context.Entry(order).State);
                }

                {
                    var order = context.Orders.First();
                    Console.WriteLine(context.Entry(order).State);
                }

                {
                    foreach (var item in context.ChangeTracker.Entries())
                    {
                        Console.WriteLine("{0}：{1}", item.Entity.GetHashCode(), item.State);
                    }
                }
            }
        }

        private static void 未修改情况的主键映射()
        {
            Console.WriteLine("未修改情况的主键映射");

            using (var context = new StudyContext())
            {
                {
                    var order = context.Orders.First();
                    Console.WriteLine(context.Entry(order).State);
                }

                {
                    var order = context.Orders.Find(1);
                    Console.WriteLine(context.Entry(order).State);
                }

                {
                    var order = context.Orders.First();
                    Console.WriteLine(context.Entry(order).State);
                }

                {
                    foreach (var item in context.ChangeTracker.Entries())
                    {
                        Console.WriteLine("{0}：{1}", item.Entity.GetHashCode(), item.State);
                    }
                }
            }
        }

        private static void 已添加情况的主键映射()
        {
            Console.WriteLine("已添加情况的主键映射");

            using (var context = new StudyContext())
            {
                {
                    var order = new Order { Id = 1, Product = "Product1" };
                    context.Orders.Add(order);
                    Console.WriteLine(context.Entry(order).State);
                }

                {
                    var order = context.Orders.Find(1);
                    Console.WriteLine(context.Entry(order).State);
                }

                {
                    var order = context.Orders.First();
                    Console.WriteLine(context.Entry(order).State);
                }

                {
                    foreach (var item in context.ChangeTracker.Entries())
                    {
                        Console.WriteLine("{0}：{1}", item.Entity.GetHashCode(), item.State);
                    }
                }
            }
        }

        private static void xxx()
        {
            Console.WriteLine("xxx");

            using (var context = new StudyContext())
            {
                var order = context.Orders.First();
                context.Entry(new Order { Id = 1, Product = "Product1" }).State = System.Data.EntityState.Added;
                context.Entry(new Order { Id = 1, Product = "Product1" }).State = System.Data.EntityState.Added;
                Console.WriteLine(context.Entry(order).State);
            }
        }
    }
}
